/**
 * 
 */
$(function() {

			$('.dialogs,.comments').slimScroll({
				height : '300px'
			});

			$('#tasks').sortable();
			$('#tasks').disableSelection();
			$('#tasks input:checkbox').removeAttr('checked').on('click',
					function() {
						if (this.checked)
							$(this).closest('li').addClass('selected');
						else
							$(this).closest('li').removeClass('selected');
					});

			var oldie = $.browser.msie && $.browser.version < 9;
			$('.easy-pie-chart.percentage')
					.each(
							function() {
								var $box = $(this).closest('.infobox');
								var barColor = $(this).data('color')
										|| (!$box.hasClass('infobox-dark') ? $box
												.css('color')
												: 'rgba(255,255,255,0.95)');
								var trackColor = barColor == 'rgba(255,255,255,0.95)' ? 'rgba(255,255,255,0.25)'
										: '#E2E2E2';
								var size = parseInt($(this).data('size')) || 50;
								$(this).easyPieChart({
									barColor : barColor,
									trackColor : trackColor,
									scaleColor : false,
									lineCap : 'butt',
									lineWidth : parseInt(size / 10),
									animate : oldie ? false : 1000,
									size : size
								});
							});

			$('.sparkline').each(
					function() {
						var $box = $(this).closest('.infobox');
						var barColor = !$box.hasClass('infobox-dark') ? $box
								.css('color') : '#FFF';
						$(this).sparkline('html', {
							tagValuesAttribute : 'data-values',
							type : 'bar',
							barColor : barColor,
							chartRangeMin : $(this).data('min') || 0
						});
					});

			$.demonPlugin.asyncData("GET", "../dash/userlogincount", null, function(data){
				if (data.status == "ok"){
					try{
						data = data.data;
						var login = 0;
						var wifi = 0;
						var perc = 0;
						for (var i in data){
							if (data[i].status == "0"){
								login = data[i].count; 
							} else if (data[i].status == "1"){
								wifi = data[i].count;
							}
						}
						perc = (isNaN(wifi/login)?perc:Math.round(wifi/login)*100) + "%";
						$("#curlogin").text(login);
						$("#curwifi").text(wifi);
						$("#loginpercent").text(perc);
					}catch (err){
						//console.log(err);
					}
					
				}
			});
			
			$.demonPlugin.asyncData("GET", "../dash/useraccountcount", null, function(data){
				if (data.status == "ok"){
					try{
						data = data.data;
						var use = 0;
						var unuse = 0;
						var perc = 0;
						for (var i in data){
							if (data[i].status == "0"){
								unuse = data[i].count; 
							} else if (data[i].status == "1"){
								use = data[i].count;
							}
						}
						perc = (isNaN(unuse/(use+unuse))?perc:Math.round(unuse/(use+unuse))*100);
						$("#accuse").text(use);
						$("#accunuse").text(unuse);
						$("#accper").text(perc);
						$("#accper2").text(perc+"%");
					}catch (err){
						//console.log(err);
					}
					
				}
			});
			
			$.demonPlugin.asyncData("GET", "../dash/userloginmoncount", null, function(data){
				if (data.status == "ok"){
					try{
						$("#monlogin").text(data.data);
					}catch (err){
						//console.log(err);
					}
				}
			});
			
			$.demonPlugin.asyncData("GET", "../dash/userdurationmoncount", null, function(data){
				if (data.status == "ok"){
					try{
						$("#monduration").text(Math.ceil(data.data/60)+"小时");
					}catch (err){
						//console.log(err);
					}
				}
			});
			
			var placeholder = $('#piechart-placeholder').css({
				'width' : '90%',
				'min-height' : '150px'
			});
			
			var data = [ {
				label : "中国移动",
				data : 1,
				color : "#68BC31"
			}, {
				label : "中国联通",
				data : 1,
				color : "#2091CF"
			}
			 , {
				label : "中国电信",
				data : 1,
				color : "#DA5430"
			}, 
			];
			
			function drawPie(data){
				$.plot(placeholder, data, {
					series : {
						pie : {
							show : true,
							tilt : 0.8,
							highlight : {
								opacity : 0.25
							},
							stroke : {
								color : '#fff',
								width : 2
							},
							startAngle : 2
						}
					},
					legend : {
						show : true,
						position : "ne",
						labelBoxBorderColor : null,
						margin : [ -30, 15 ]
					},
					grid : {
						hoverable : true,
						clickable : true
					},
					tooltip : true, 
					tooltipOpts : {
						content : "%s : %y.1",
						shifts : {
							x : -30,
							y : -50
						}
					}
				});
			}

			drawPie(data);
			
			$.demonPlugin.asyncData("GET", "../dash/useraccountoptcount", null, function(rdata){
				if (rdata.status == "ok"){
					try{
						var rddata = rdata.data;
						for (var i in rddata){
							if (rddata[i].status == "10010"){
								data[1].data = rddata[i].count;
								$("#piecu").text(data[1].data) ;
							}
							else if (rddata[i].status == "10086"){
								data[0].data = rddata[i].count;
								$("#piecm").text(data[0].data);
							}
							else if (rddata[i].status == "189"){
								data[2].data = rddata[i].count;
								$("#piect").text(data[2].data);
							}
						}
						drawPie(data);
					}catch (err){
						//console.log(err);
					}
					
				}
			});
			
			var $tooltip = $(
					"<div class='tooltip top in' style='display:none;'><div class='tooltip-inner'></div></div>")
					.appendTo('body');
			placeholder.data('tooltip', $tooltip);
			var previousPoint = null;

			placeholder.on('plothover', function(event, pos, item) {
				if (item) {
					if (previousPoint != item.seriesIndex) {
						previousPoint = item.seriesIndex;
						var tip = item.series['label'] + " : "
								+ Number(item.series['percent']).toFixed(2) + '%';
						$(this).data('tooltip').show().children(0).text(tip);
					}
					$(this).data('tooltip').css({
						top : pos.pageY + 10,
						left : pos.pageX + 10
					});
				} else {
					$(this).data('tooltip').hide();
					previousPoint = null;
				}
			});

			var sales_charts = $('#sales-charts').css({
				'width' : '100%',
				'height' : '220px'
			});
			
			function drawPoints(last, current){
				$.plot("#sales-charts", [ {
					label : "本月上网时长",
					data : current
				}, {
					label : "上月上网时长",
					data : last
				}], {
					hoverable : true,
					shadowSize : 0,
					series : {
						lines : {
							show : true
						},
						points : {
							show : true
						}
					},
					xaxis : {
						ticks : [1,5,10,15,20,25,30],
						tickFormatter: function(axis){
							return axis + "日";
						},
						tickLength : 0
					},
					yaxis : {
						ticks : 10,
						min : 0,
						tickFormatter: function(axis){
							return axis + "分钟";
						},
						//max : 10,
						tickDecimals : 0
					},
					grid : {
						backgroundColor : {
							colors : [ "#fff", "#fff" ]
						},
						borderWidth : 1,
						borderColor : '#555'
					}
				});				
			}
			
			var d1 = [[0,0]];
			drawPoints(d1, d1);
			$.demonPlugin.asyncData("GET", "../dash/userdaymoncount", null, function(data){
				if (data.status == "ok"){
					try{
						drawPoints(data.data.last, data.data.current);
					}catch (err){
						//console.log(err);
					}
					
				}
			});
			
			$('[data-rel="tooltip"]').tooltip();
			
			$("#contentFrame",window.parent.document).height($(document.body).height());
		});